@echo off
setlocal enabledelayedexpansion

REM ==================================================
REM RDIOT Demo - Fire IoT Platform Testing Tool (Windows)
REM ==================================================

if "%1"=="" goto :help
if "%1"=="help" goto :help
if "%1"=="test" goto :test_unit
if "%1"=="test-unit" goto :test_unit
if "%1"=="test-quick" goto :test_quick
if "%1"=="test-coverage" goto :test_coverage
if "%1"=="build" goto :build
if "%1"=="fmt" goto :fmt
if "%1"=="vet" goto :vet
if "%1"=="clean" goto :clean
if "%1"=="setup" goto :setup

echo Unknown command: %1
goto :help

:help
echo.
echo RDIOT Demo - Fire IoT Platform Testing Tool
echo.
echo Usage:
echo   test.bat ^<command^>
echo.
echo Available commands:
echo   help          Show this help message
echo   test          Run unit tests (default)
echo   test-unit     Run unit tests
echo   test-quick    Quick test (no coverage)
echo   test-coverage Generate test coverage report
echo   build         Build application
echo   fmt           Format code
echo   vet           Run go vet checks
echo   clean         Clean build files
echo   setup         Setup test environment
echo.
echo Examples:
echo   test.bat test-quick
echo   test.bat test-coverage
echo.
goto :end

:setup
echo Setting up test environment...
if not exist "coverage" mkdir coverage
if not exist "logs" mkdir logs
if not exist "bin" mkdir bin
echo Test environment setup completed
goto :end

:fmt
echo Formatting code...
go fmt ./...
go mod tidy
echo Code formatting completed
goto :end

:vet
echo Running go vet checks...
go vet ./...
if %errorlevel% equ 0 (
    echo Go vet checks passed
) else (
    echo Go vet checks failed
)
goto :end

:build
echo Building application...
go build -o bin\rdiot-demo.exe .\cmd\server
if %errorlevel% equ 0 (
    echo Build successful: bin\rdiot-demo.exe
) else (
    echo Build failed
)
goto :end

:test_quick
echo Running quick tests (no coverage)...
go test -timeout 60s ./tests/unit/... -v
if %errorlevel% equ 0 (
    echo Quick tests completed
) else (
    echo Tests failed
)
goto :end

:test_unit
echo Running unit tests...
call :setup
go test -timeout 300s -race -coverprofile=coverage\coverage.out ./tests/unit/... ./internal/eav/domain/... ./internal/eav/eav_service/... ./internal/eav/repository/... -v
if %errorlevel% equ 0 (
    echo Unit tests completed
) else (
    echo Unit tests failed
)
goto :end

:test_coverage
echo Generating test coverage report...
call :test_unit
if exist "coverage\coverage.out" (
    go tool cover -html=coverage\coverage.out -o coverage\coverage.html
    go tool cover -func=coverage\coverage.out | findstr "total:"
    echo.
    echo Coverage report generated:
    echo   - coverage\coverage.out
    echo   - coverage\coverage.html
    echo.
    echo Open coverage\coverage.html in browser to view detailed report
) else (
    echo Coverage file not found, please run unit tests first
)
goto :end

:clean
echo Cleaning build files...
if exist "bin" rmdir /s /q bin
if exist "coverage" rmdir /s /q coverage
if exist "logs\test_*.log" del /q logs\test_*.log
echo Cleanup completed
goto :end

:end 